local tongli = fk.CreateSkill {

  name = "joy__tongli",

  tags = {  },

}



tongli:addEffect(fk.TargetSpecified, {
  name = "joy__tongli",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(tongli.name) and player.phase == Player.Play and data.firstTarget and
    data.extra_data and data.extra_data.tongli_target and
    not table.contains(data.card.skillNames, tongli.name) and player:getMark("@joy__tongli-phase") > 0 and
    data.card.type ~= Card.TypeEquip and data.card.sub_type ~= Card.SubtypeDelayedTrick and
    not (table.contains({"peach", "analeptic"}, data.card.trueName) and
    table.find(player.room.alive_players, function(p) return p.dying end)) then
      local suits = {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        table.insertIfNeed(suits, Fk:getCardById(id).suit)
      end
      return #suits == player:getMark("@joy__tongli-phase")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.general == "joy__zhangxuan" then
      player.general = "joy_skin__zhangxuan"
      player.room:broadcastProperty(player, "general")
      player.room:delay(500)
    end
    data.extra_data = data.extra_data or {}
    data.extra_data.tongli = {
      from = player.id,
      tos = data.extra_data.tongli_target,
      times = player:getMark("@joy__tongli-phase")
    }
  end,
})
tongli:addEffect(fk.PreCardUse, {
  refresh_events = {fk.PreCardUse},
  can_refresh = function(self, event, target, player, data)
    return player == target and player:hasSkill(self, true) and player.phase == Player.Play and
    not table.contains(data.card.skillNames, tongli.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@joy__tongli-phase", 1)
    if type(data.tos) == "table" then
      data.extra_data = data.extra_data or {}
      data.extra_data.tongli_target = table.simpleClone(data.tos)
    end
  end,
})

tongli:addEffect(fk.CardUseFinished, {
  name = "#joy__tongli_delay",
  events = {fk.CardUseFinished},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if data.extra_data and data.extra_data.tongli and not player.dead then
      local dat = table.simpleClone(data.extra_data.tongli)
      if dat.from == player.id then
        local use = parseTongliUseStruct(player, data.card.name, dat.tos)
        if use then
          event:setCostData(self, use)
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = table.simpleClone(data.extra_data.tongli)
    local use = table.simpleClone(event:getCostData(self))
    local room = player.room
    player:broadcastSkillInvoke("joy__tongli")
    for _ = 1, dat.times, 1 do
      room:useCard(use)
      if player.dead then break end
    end
    if player.general == "joy_skin__zhangxuan" then
      room:notifySkillInvoked(player,tongli.name,"drawcard")
      player.general = "joy__zhangxuan"
      room:broadcastProperty(player, "general")
    end
  end,
})

return tongli